package com.qf.smartdevice.controller;

import com.github.pagehelper.PageInfo;
import com.qf.smartdevice.cache.AreaCache;
import com.qf.smartdevice.constans.ResultCode;
import com.qf.smartdevice.dto.R;
import com.qf.smartdevice.exceptions.AddDataException;
import com.qf.smartdevice.pojo.Areas;
import com.qf.smartdevice.service.IAreasService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;


@RestController
@RequestMapping("/areas")
public class AreasController {
    @Autowired
    private IAreasService areasService;

    //   获取所有的父区域   也就是没有父区域的区域就是父区域
//    元数据  描述数据的数据叫元数据
    @GetMapping("/areas/parent")
    public R getAllParenArea() {
        List<Areas> allParentArea = areasService.getAllParentArea();

        return R.setOk(allParentArea);
    }

    @GetMapping("/area/parent/{parentId}")
    public R getAllParenArea(@PathVariable Long parentId) {
        List<Areas> allAreasByParentId = areasService.getAllAreasByParentId(parentId);

        return R.setOk(allAreasByParentId);
    }

    @GetMapping("/areas")
    public R getAllArea(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit, String areaname, @RequestParam(defaultValue = "-100") Long status) {
        PageInfo<Areas> allParentArea = areasService.findAllArea(page, limit, areaname, status);

        System.out.println("ssssssss" + allParentArea);
        return R.setOk(allParentArea);
    }

    @DeleteMapping("/areas")
    public R deleteByIdAreas(int[] ids) {
        int i = areasService.deleteByIdArea(ids);
        return R.setOk(null);
    }

    @PostMapping("/area")
    public R insertArea(@RequestBody Areas areas) {

        if (areas == null || areas.isNull()) {
            throw new AddDataException("数据不完整", ResultCode.NULL_DATA);
        }
        int i = areasService.insertArea(areas);
        return R.setOk(null);
    }

    @PutMapping("/area")
    public R updateArea(@RequestBody Areas areas) {
        if (areas == null || areas.isNull()) {
            throw new AddDataException("数据不完整", ResultCode.NULL_DATA);
        }
        int i = areasService.updateArea(areas);
        return R.setOk(null);
    }

}
